package no.hio.wsprovider.services;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

import junit.framework.TestCase;
import no.hio.wsprovider.dao.StockItemDAO;
import no.hio.wsprovider.vo.StockItem;

import org.junit.Before;
import org.junit.Test;

public class StockItemServiceImplTestCase extends TestCase implements StockItemDAO
{
	StockItem stockItem1, stockItem2;
	StockItemServiceImpl stockItemService;
	
	/* (non-Javadoc)
	 * @throws java.lang.Exception
	 * @see junit.framework.TestCase#setUp()
	 */
	@Before
	public void setUp() throws Exception
	{
		stockItem1 = new StockItem();
		stockItem2 = new StockItem();
		
		stockItem1.setProductID(0);
		stockItem2.setProductID(2);
		stockItem1.setNumInStock(12);
		stockItem2.setNumInStock(13);
		stockItem1.setStockLocation("hylle3");
		stockItem2.setStockLocation("hylle4");
		stockItemService = new StockItemServiceImpl();
		stockItemService.setStockItemDAO(this);
	}

	@Test
	public void testInsertStockItem()
	{
		stockItemService.insertStockItem(stockItem1);
		stockItemService.insertStockItem(stockItem2);
	}

	@Test
	public void testUpdateStockItem()
	{
		stockItemService.updateStockItem(stockItem1);
		stockItemService.updateStockItem(stockItem2);
	}
	
	@Test
	public void testFindAllStockItems()
	{
		assertEquals(2, stockItemService.findAllStockItems().size());
	}

	@Test
	public void testFindByProductID()
	{
		assertEquals(12, stockItemService.findByProductID(1).getNumInStock());
	}

	@Override
	public List<StockItem> findAll()
	{
		// TODO Auto-generated method stub
		List<StockItem> retur = new ArrayList<StockItem>();
		retur.add(stockItem1);
		retur.add(stockItem2);
		return retur;
	}

	@Override
	public StockItem findByProductID(int productID)
	{
		return stockItem1;
	}

	@Override
	public void insert(StockItem stockItem) throws UnsupportedOperationException
	{
		if(stockItem.getProductID()==0) 
			throw new UnsupportedOperationException();
	}

	@Override
	public void remove(StockItem stockItem) throws NoSuchElementException
	{
		if(stockItem.getProductID()==0) 
			throw new NoSuchElementException();
	}

	@Override
	public void update(StockItem stockItem)
	{
		// TODO Auto-generated method stub
		
	}

}
